package cn.xxstudy.compose.material

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

/**
 * @author ：wuxinxi on 2021/3/4 .
 * @packages ：cn.xxstudy.compose.material .
 * TODO:BottomDrawer 从底部弹出
 */
class SimpleBottomDrawerActivity : AppCompatActivity() {
    @ExperimentalMaterialApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            BottomDrawerComponent()
        }
    }

}

@ExperimentalMaterialApi
@Preview
@Composable
fun BottomDrawerComponent() {
    val drawerState = rememberBottomDrawerState(initialValue = BottomDrawerValue.Closed)
    val scope = rememberCoroutineScope()
    BottomDrawer(
        drawerState = drawerState,
        drawerContent = {
            Button(modifier = Modifier
                .align(Alignment.CenterHorizontally)
                .padding(top = 16.dp),
                onClick = { scope.launch { drawerState.close() } },
                content = { Text(text = "Close Drawer") })
            for (i in 0..5) {
                Spacer(modifier = Modifier.padding(vertical = 3.dp))
                Text(text = "Item $i >>>")
            }
        },
        content = {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                Text(text = if (drawerState.isClosed) "▲▲▲ Pull up ▲▲▲" else "▼▼▼ Drag down ▼▼▼")
                Spacer(modifier = Modifier.height(20.dp))
                Button(onClick = { scope.launch { drawerState.open() } }) {
                    Text("Click to open")
                }

            }
        }
    )
}